<?php

namespace app\modules\api\controllers;

use app\models\User;
use app\modules\Finance\models\FinanceSearch;
use app\modules\Order\models\Order;
use Yii;
use app\modules\Product\models\Product;
use app\modules\Product\models\ProductRoom;
use yii\web\Controller;
use app\modules\Order\models\OrderRoom;
use yii\filters\VerbFilter;
use yii\filters\AccessControl;
use app\modules\Order\models\OrderPriceType;
use app\modules\Order\models\OrderPrice;

/**
 * Default controller for the `api` module
 */
class DefaultController extends Controller
{


    public function behaviors()
    {
        return [
            'verbs' => [
                'class' => VerbFilter::className(),
                'actions' => [
                    'delete' => ['post'],
                ],
            ],
//            'access' => [
//                'class' => AccessControl::className(),
//                'rules' => [
//                    [
//                        'allow' => true,
////                        'actions' => ['product-index','product-room','product-order','one-product','one-order'],
//                        'roles' => ['@'],
//                        //'matchCallback' => function ($rule, $action) {
//                        //	return  (Yii::$app->user->identity->username == 'admin') ? true : false;
//                        //}
//                    ],
//                    [
//                        'allow' => true,
//                        'actions' => ['neword','word','file-upload','del-img'],
//                        'roles' => ['?'],
//                    ],
//                ],
//            ],
        ];
    }
    /**
     * Renders the index view for the module
     * @return string
     */
    /**
     * Lists all Product models.
     * @param integer $start_time
     * @param integer $end_time
     * @return mixed
     */
    public function actionProductIndex()
    {
        $time = Yii::$app->request->get();
        if(!isset($time['token'])){
            return json_encode(['code'=>10,'message'=>'Token未设置']);
        }
        Yii::$app->token->tokenApi($time['token']);
        if(empty($time)){
            return json_encode(['code'=>11,'message'=>'请选择时间']);
        }
        if(empty($time['start_time'])) {
            return json_encode(['code'=>12,'message'=>'开始时间不能为空']);
        }
        if(empty($time['end_time'])){
            return json_encode(['code'=>13,'message'=>'结束时间不能为空']);
        }

        $data = Product::find()
            ->select(['id','name','go_time','return_time','type','status','stock'])
            ->where(['between','go_time',$time['start_time'],$time['end_time']])
            ->asArray()
            ->all();
        if($data){
            return json_encode(['code'=>1,'data'=>$data,'message'=>'请求成功']);
        }
        return json_encode(['code'=>14,'message'=>'你选择的这个时间段没有团组']);
    }


    /**
     * Lists all room models.
     * @param integer $start_time
     * @param integer $end_time
     * @return mixed
     */

    public function actionProductRoom()
    {
        $data = Yii::$app->request->get();
        if(!isset($data['token'])){
            return json_encode(['code'=>10,'message'=>'Token未设置']);
        }
        Yii::$app->token->tokenApi($data['token']);

        $id = $data['id'];

        if(empty($id)) {
            return json_encode(['code'=>11,'message'=>'id不能为空']);
        }
        $data = Product::findOne($id);
        if(empty($data)){
            return json_encode(['code'=>12,'message'=>'暂无团队信息']);
        }
        if($data['stock'] == 1){
            $room = ProductRoom::find()
                ->joinWith(['room'])
                ->Where(['>','show',0])
                ->andWhere(['product_id'=>$data['id']])
                ->asArray()->all();

            if(empty($room)){
                return json_encode(['code'=>13,'message'=>'暂无库存信息']);
            }
            foreach ($room  as $k => $v) {
                $num = OrderRoom::find()->where(['product_id'=>$v['product_id'], 'room_id'=>$v['id'] ,'status'=>1, 'stock'=>1])->count();
                $list[$k]['id'] = $v['id'];
                $list[$k]['room_id'] = $v['room']['id'];
                $list[$k]['name'] = $v['room']['name'].' '.$v['room']['code'].' '.$v['people'].'人间';
                $list[$k]['price'] = $v['price'];
                $list[$k]['inside_price'] = $v['inside_price'];
                $list[$k]['people'] = $v['people'];
                $list[$k]['sur'] = $v['num']-$num;
                $list[$k]['num'] = $v['num'];
                $list[$k]['sell_out'] = $v['sell_out'];
            }
            return json_encode(['code'=>1,'data'=>$list,'message'=>'请求成功']);
        }else{
            return json_encode(['code'=>14,'message'=>'不支持自定义格式']);
        }
//        if ($data['stock'] == 2){
//            $obj = json_decode($data['rooms'],true);
//            if(empty($obj)){
//                return json_encode(['code'=>0,'data'=>'','message'=>'暂无库存信息']);
//            }
//            foreach ($obj as $k => $v) {
//                $num = OrderRoom::find()->where(['product_id'=>$data['id'], 'room_id'=>$k ,'status'=>1, 'stock'=>1])->count();
//                $two[$k]['name'] = $v['name'];
//                $two[$k]['price'] = $v['price'];
//                $two[$k]['inside_price'] = $v['inside_price'];
//                $two[$k]['price2'] = $v['price2'];
//                $two[$k]['inside_price2'] = $v['inside_price2'];
//                $two[$k]['sur'] = $v['num']-$num;
//                $two[$k]['num'] = $v['num'];
//            }
//            return json_encode(['code'=>1,'data'=>$two,'message'=>'请求成功']);
//        }
    }

    /**
     * Lists all order models.
     * @param integer $start_time
     * @param integer $end_time
     * @return mixed
     */
    public function actionProductOrder()
    {
        $data = Yii::$app->request->get();
        if(!isset($data['token'])){
            return json_encode(['code'=>10,'message'=>'Token未设置']);
        }
        Yii::$app->token->tokenApi($data['token']);

        $id = $data['id'];

        if(empty($id)) {
            return json_encode(['code'=>11,'message'=>'团队id不能为空']);
        }
        $data = Order::find()->joinWith(['member' => function($q){ $q->joinWith(['profile']); }])->andWhere(['product_id'=>$id,'status'=>[1,2,3,4, 11 ,12]])->andWhere(['is','dentification',null])->orderBy('created_at desc')->asArray()->all();
        if(empty($data)) {
            return json_encode(['code'=>12,'message'=>'暂时没有订单']);
        }
        foreach ($data as $k => $v){
            $success = new Order();
            $financeSuccess = $success->financeSuccess;
            $priceSum = OrderPrice::find()->where(['order_id'=>$v['id'],'status'=>1])->sum('total_price');
            $list[$k]['id'] = $v['id'];
            $list[$k]['user_id'] = $v['member']['profile']['user_id'];
            $list[$k]['name'] = $v['member']['profile']['name'];
            $list[$k]['created_at'] = $v['created_at'];
            $list[$k]['updated_at'] = $v['updated_at'];
            $list[$k]['code'] = $v['code'];
            $list[$k]['username'] = $v['username'];
            $list[$k]['phone'] = $v['phone'];
            $list[$k]['company'] = $v['company'];
            $list[$k]['remark'] = $v['remark'];
            $list[$k]['total_price'] = $v['total_sale'];
            $list[$k]['sale_price'] = $v['sale_price'];
            $list[$k]['pricesum'] = $priceSum;
            $list[$k]['price'] = $v['price'];
            $list[$k]['financesuccess'] = $financeSuccess;
            $list[$k]['status'] = $v['status'];
            $list[$k]['assign_id'] = $v['assign_id'];
            $list[$k]['room'] = [];
            $product = Product::find()->where(['id'=>$v['product_id']])->asArray()->all();
//            if($product[0]['stock'] == 2) {
//                $obj = json_decode($product[0]['rooms'],true);
//                if($v['status']<10) {
//                    $order_room = OrderRoom::find()->joinWith(['orderRoomCustomerapi' => function($q){ $q->joinWith(['orderCustomer']); }])->andWhere(['order_room.order_id'=>$v['id'],'order_room.status'=>1])->asArray()->all();
//                }else{
//                    $order_room = OrderRoom::find()->joinWith(['orderRoomCustomerapi' => function($q){ $q->joinWith(['orderCustomer']); }])->andWhere(['order_room.order_id'=>$v['id']])->asArray()->all();
//                }
//                foreach ($order_room as $k2 => $v2){
//
//                    $list[$k]['room'][$k2]['name'] = $obj[$v2['room_id']]['name'];
//                    $list[$k]['room'][$k2]['code'] = !empty($v2['code'])?$v2['code']:'暂无房号';
//                    $list[$k]['room'][$k2]['member_name'] = [];
//                    foreach ($v2['orderRoomCustomerapi'] as $k3 => $v3){
//                        if ($v3['status'] == 1){
//                            $list[$k]['room'][$k2]['member_name'][$k3]['id'] = $v3['orderCustomer']['id'];
//                            $list[$k]['room'][$k2]['member_name'][$k3]['names'] = $v3['orderCustomer']['first_name'].$v3['orderCustomer']['last_name'];
//                        }
//                    }
//                }
//            }
            if($product[0]['stock'] == 1){
                if($v['status']<10) {
                    $order_room = OrderRoom::find()->joinWith(['orderRoomCustomerapi' => function($q){ $q->joinWith(['orderCustomer']); }])->joinWith(['productRoom' => function($q){ $q->joinWith(['room']); }])->andWhere(['order_room.order_id'=>$v['id'],'order_room.status'=>1])->asArray()->all();
                }else{
                    $order_room = OrderRoom::find()->joinWith(['orderRoomCustomerapi' => function($q){ $q->joinWith(['orderCustomer']); }])->joinWith(['productRoom' => function($q){ $q->joinWith(['room']); }])->andWhere(['order_room.order_id'=>$v['id']])->asArray()->all();
                }
                foreach ($order_room as $k2 => $v2){
                    $list[$k]['room'][$k2]['id'] = $v2['productRoom']['id'];
                    $list[$k]['room'][$k2]['order_room_id'] = $v2['id'];
                    $list[$k]['room'][$k2]['room_id'] = $v2['productRoom']['room']['id'];
                    $list[$k]['room'][$k2]['name'] = $v2['productRoom']['room']['name'].' '.$v2['productRoom']['room']['code'].' '.$v2['productRoom']['people'].'人间';
                    $list[$k]['room'][$k2]['code'] = !empty($v2['code'])?$v2['code']:'暂无房号';
                    $list[$k]['room'][$k2]['price'] = $v2['price'];
                    $list[$k]['room'][$k2]['commiss_price'] = $v2['commiss_price'];
                    $list[$k]['room'][$k2]['people'] = $v2['productRoom']['people'];
                    $list[$k]['room'][$k2]['member_name'] = [];
                    foreach ($v2['orderRoomCustomerapi'] as $k3 => $v3){
                        if ($v3['status'] == 1){
                            $list[$k]['room'][$k2]['member_name'][$k3]['id'] = $v3['orderCustomer']['id'];
                            $list[$k]['room'][$k2]['member_name'][$k3]['names'] = $v3['orderCustomer']['first_name'].$v3['orderCustomer']['last_name'];
                        }
                    }

                }
            }else{
                return json_encode(['code'=>13,'message'=>'不支持自定义格式']);
            }

        }
        return json_encode(['code'=>1,'data'=>$list,'message'=>'请求成功']);

    }


    public function actionOneOrder()
    {
        $data = Yii::$app->request->get();

        if(empty($data['id'])){
            return json_encode(['code'=>11,'message'=>'订单id不能为空']);

        }

        if(!isset($data['token'])){
            return json_encode(['code'=>10,'message'=>'Token未设置']);
        }

        Yii::$app->token->tokenApi($data['token']);

//        $order = Order::find()->joinWith(['member' => function($q){ $q->joinWith(['profile']); }])->andWhere(['order.id'=>$data['id'],'order.status'=>[1,2,3,4, 11 ,12]])->andWhere(['is','order.dentification',null])->orderBy('created_at desc')->asArray()->all();
        $order = Order::find()->joinWith(['member' => function($q){ $q->joinWith(['profile']); }])->andWhere(['order.id'=>$data['id'],'order.status'=>[1,2,3,4, 11 ,12]])->orderBy('created_at desc')->asArray()->all();

        if(empty($order)) {
            return json_encode(['code'=>12,'message'=>'暂时没有订单']);
        }

        foreach ($order as $k => $v){
            $success = new Order();
            $financeSuccess = $success->financeSuccess;
            $priceSum = OrderPrice::find()->where(['order_id'=>$v['id'],'status'=>1])->sum('total_price');
            $list[$k]['id'] = $v['id'];
            $list[$k]['user_id'] = $v['member']['profile']['user_id'];
            $list[$k]['name'] = $v['member']['profile']['name'];
            $list[$k]['created_at'] = $v['created_at'];
            $list[$k]['updated_at'] = $v['updated_at'];
            $list[$k]['code'] = $v['code'];
            $list[$k]['username'] = $v['username'];
            $list[$k]['phone'] = $v['phone'];
            $list[$k]['company'] = $v['company'];
            $list[$k]['remark'] = $v['remark'];
            $list[$k]['total_price'] = $v['total_sale'];
            $list[$k]['sale_price'] = $v['sale_price'];
            $list[$k]['pricesum'] = $priceSum;
            $list[$k]['price'] = $v['price'];
            $list[$k]['financesuccess'] = $financeSuccess;
            $list[$k]['status'] = $v['status'];
            $list[$k]['assign_id'] = $v['assign_id'];
            $list[$k]['room'] = [];
            $list[$k]['surcharge'] = [];
            $product = Product::find()->where(['id'=>$v['product_id']])->asArray()->all();
            if($product[0]['stock'] == 1){
                if($v['status']<10) {
                    $order_room = OrderRoom::find()->joinWith(['orderRoomCustomerapi' => function($q){ $q->joinWith(['orderCustomer']); }])->joinWith(['productRoom' => function($q){ $q->joinWith(['room']); }])->andWhere(['order_room.order_id'=>$v['id'],'order_room.status'=>1])->asArray()->all();
                }else{
                    $order_room = OrderRoom::find()->joinWith(['orderRoomCustomerapi' => function($q){ $q->joinWith(['orderCustomer']); }])->joinWith(['productRoom' => function($q){ $q->joinWith(['room']); }])->andWhere(['order_room.order_id'=>$v['id']])->asArray()->all();
                }
                foreach ($order_room as $k2 => $v2){
                    $list[$k]['room'][$k2]['id'] = $v2['productRoom']['id'];
                    $list[$k]['room'][$k2]['order_room_id'] = $v2['id'];
                    $list[$k]['room'][$k2]['room_id'] = $v2['productRoom']['room']['id'];
                    $list[$k]['room'][$k2]['name'] = $v2['productRoom']['room']['name'].' '.$v2['productRoom']['room']['code'].' '.$v2['productRoom']['people'].'人间';
                    $list[$k]['room'][$k2]['code'] = !empty($v2['code'])?$v2['code']:'暂无房号';
                    $list[$k]['room'][$k2]['price'] = $v2['price'];
                    $list[$k]['room'][$k2]['commiss_price'] = $v2['commiss_price'];
                    $list[$k]['room'][$k2]['member_name'] = [];
                    foreach ($v2['orderRoomCustomerapi'] as $k3 => $v3){
                        if ($v3['status'] == 1){
                            $list[$k]['room'][$k2]['member_name'][$k3]['id'] = $v3['orderCustomer']['id'];
                            $list[$k]['room'][$k2]['member_name'][$k3]['names'] = $v3['orderCustomer']['first_name'].$v3['orderCustomer']['last_name'];
                        }
                    }

                }

                $price = OrderPrice::find()->joinWith(['typeObj'])->where(['order_price.order_id'=>$v['id'], 'order_price.status'=>1])->asArray()->all();
                if(!empty($price)){
                    foreach ($price as $k4 => $v4){
                        $list[$k]['surcharge'][$k4]['id'] = $v4['id'];
                        $list[$k]['surcharge'][$k4]['name'] = $v4['typeObj']['name'];
                        $list[$k]['surcharge'][$k4]['price'] = $v4['price'];
                        $list[$k]['surcharge'][$k4]['num'] = $v4['num'];
                        $list[$k]['surcharge'][$k4]['total_price'] = $v4['total_price'];
                    }
                }

            }else{
                return json_encode(['code'=>13,'message'=>'不支持自定义格式']);
            }

        }

        return json_encode(['code'=>1,'data'=>$list,'message'=>'请求成功']);



    }



    public function actionOneProduct()
    {
        $data = Yii::$app->request->get();

        if(empty($data['id'])){
            return json_encode(['code'=>11,'message'=>'团队id不能为空']);
        }

        if(!isset($data['token'])){
            return json_encode(['code'=>10,'message'=>'Token未设置']);
        }

        Yii::$app->token->tokenApi($data['token']);


        $product = Product::find()->select(['id','name','go_time','return_time','type','status','cruise_id','day','stock'])->where(['id'=>$data['id']])->asArray()->one();

        if($product){
            return json_encode(['code'=>1,'data'=>$product,'message'=>'请求成功']);
        }else{
            return json_encode(['code'=>12,'message'=>'团队id不存在']);
        }

    }


    public function actionOrderType()
    {
        $data = Yii::$app->request->get();

        if(empty($data['id'])){
            return json_encode(['code'=>11,'message'=>'团队id不能为空']);
        }

        if(!isset($data['token'])){
            return json_encode(['code'=>10,'data'=>'','message'=>'Token未设置']);
        }

        Yii::$app->token->tokenApi($data['token']);

        $data = OrderPriceType::find()->select(['id','name','desc','price','status','type','created_at'])->where(['product_id'=>$data['id']])->asArray()->all();

        if($data){
            return json_encode(['code'=>1,'data'=>$data,'message'=>'请求成功']);
        }else{
            return json_encode(['code'=>12,'message'=>'数据为空']);
        }
    }



}
